<template>
  <div align-center>
    <el-page-header content="商品列表"></el-page-header>
    <div class="tag-group">
      <span class="tag-group__title">商品列表:</span>
      <el-tag
        v-for="item in tag"
        :key="item.label"
        :type="item.type"
        effect="plain"
        style="margin-top: 50px;"
      >{{ item.label }}</el-tag>
    </div>
    <!--查询表单-->
    <el-form ref="searchMap" :model="searchMap" label-width="80px">
      <el-form-item label="输入搜索">
        <el-input v-model="searchMap.id" placeholder="商品货号"></el-input>
      </el-form-item>
      <el-form-item label="商品名称">
        <el-input v-model="searchMap.name" placeholder="商品名称"></el-input>
      </el-form-item>
      <el-button type="primary" @click="onSubmit">查询</el-button>
    </el-form>
    <!--表格-->
    <el-table :data="tableData" height="600px" border style="width: 100%">
      <el-table-column label="编号" width="80" prop="id" height="200px"></el-table-column>
      <el-table-column label="商品图片" width="180" prop="image">
        <template slot-scope="scope">
          <img :src="scope.row.skuImage" width="160px" height="80px" />
        </template>
      </el-table-column>
      <el-table-column label="商品名称" width="200" prop="name"></el-table-column>
      <el-table-column label="价格/货号" width="160" prop="sn"></el-table-column>
      <el-table-column label="标签" width="150" prop="isMarketable">
        <template slot-scope="scope">
          <el-switch
            :value="scope.row.isMarketable"
            inactive-text="上架"
            disabled
            active-value="1"
            inactive-value="0"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="SKU库存" width="80" prop="num"></el-table-column>
      <el-table-column label="销量" width="80" prop="saleNum"></el-table-column>
      <el-table-column label="审核状态" width="80" prop="status">
        <template slot-scope="scope">
          <span v-if="scope.row.status==1">以审核</span>
          <a v-else>审核详情</a>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template slot-scope="scope">
          <el-button size="mini" @click="querySku(scope.$index, scope.row)">查看</el-button>
          <el-button size="mini" type="danger" @click="handleEdit(scope.row)">编辑</el-button>
          <br />
          <el-button size="mini" @click="queryLog(scope.row)">日志</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.$index,scope .row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="pageSizeList"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    ></el-pagination>

    <el-dialog title="详情" :visible.sync="dialogFormVisible">
      <el-form :model="spuForm">
        <el-form-item label="名称" :label-width="formLabelWidth">
          <el-input disabled="disabled" v-model="spuForm.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="库存数量" :label-width="formLabelWidth">
          <el-input disabled="disabled" v-model="spuForm.num" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="是否上架" :label-width="formLabelWidth">
          <el-switch
            v-model="spuForm.isMarketable"
            active-color="#13ce66"
            inactive-color="#ff4949"
            active-value="1"
            inactive-value="0"
            disabled="disabled"
          ></el-switch>
        </el-form-item>
        <el-form-item label="审核状态" :label-width="formLabelWidth">
          <el-switch
            v-model="spuForm.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            active-value="1"
            inactive-value="0"
            disabled="disabled"
          ></el-switch>
        </el-form-item>
        <el-form-item label="所属模板" :label-width="formLabelWidth">
          <template>
            <el-select disabled="disabled" id="slt" filterable name="templateId" style="width:150px" v-model="spuForm.templateId">
              <el-option v-for="temp in tempList"
               :key="temp.id"
               :value="temp.id"
               :label="temp.name"></el-option>
            </el-select>
          </template>
        </el-form-item>
        <el-form-item label="图片" :label-width="formLabelWidth">
         <img style="width:160px;height:80px;" :src="spuForm.skuImage"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="dialogFormVisible = false; spuForm={}">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      dialogFormVisible: false,
      formLabelWidth: '175px',
      tempList: [],
      tag: [],
      spu: {},
      searchMap: {},
      tableData: [],
      currentPage: 1,
      pageSize: 10,
      pageSizeList: [5, 10, 20, 30],
      total: 0,
      spuForm: {}
    }
  },
  methods: {
    async getTempList() {
      const { data: res } = await this.$http.get("goods/template/findAll");
      this.tempList = res.data;
    },
    // 查看详情
    async querySku (index, rows) {
            const { data: res } = await this.$http.get(`goods/spu/findById/${rows.id}`);
            this.spuForm = rows;
            this.spuForm.templateId = res.data.spu.templateId
            this.dialogFormVisible = true
    },
    // 查询日志
    queryLog () {},
    // 编辑修改
    handleEdit () {

    },
    // 删除
    handleDelete (index, spu) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        spu.isDelete = '1'
        this.$http.post(`goods/spu/updateIsDelete`, spu)
        this.getSkuList()
        this.$message({
          type: 'success',
          message: '删除成功!'
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 按条件查询
    onSubmit () {
      this.getSkuList()
    },
    // 查询表格所需数据
    async getSkuList () {
      const { data: res } = await this.$http.post(`goods/spu/selectTable/${this.currentPage}/${this.pageSize}`,
        this.searchMap
      );
      this.tableData = res.data.rows;
      this.total = res.data.total;
    },
    async handleSizeChange (val) {
      this.pageSize = val;
      this.getSkuList();
    },
    async handleCurrentChange (val) {
      this.currentPage = val;
      this.getSkuList();
    }

  },
  async created () {
    const { data: res } = await this.$http.get("goods/spu/countSpu")
    this.spu = res.data
    this.tag = [{
      type: '', label: '全部商品( ' + this.spu.allSpu + ')'
    },
    { type: 'success', label: '已上架( ' + this.spu.isMarketable0 + ') ' },
    { type: 'info', label: '未上架( ' + this.spu.ismarketable1 + ') ' },
    { type: 'danger', label: '待审核( ' + this.spu.status0 + ') ' },
    { type: 'warning', label: '未通过( ' + this.spu.status2 + ') ' }]
    this.getTempList()
    // 初始化表格
    this.getSkuList();
  }
}
</script>

<style  scoped>
.el-main[data-v-c1f1971a] {
  background-color: #e9eef3;
  color: #333;
  text-align: center;
  line-height: 0px;
}
</style>
